<template>
  <div>
    <el-form
      ref="roleForm"
      :model="roleForm"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="roleForm.name" />
      </el-form-item>
      <el-form-item label="角色描述">
        <el-input v-model="roleForm.description" />
      </el-form-item>
    </el-form>
    <!-- 底部 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" @click="hSuccess">取消</el-button>
        <el-button size="small" type="primary" @click="hSubmit">确定</el-button>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { addRole, updateRole } from '../../api/setting'
export default {
  name: '',
  props: {
    // addDialogVisible: {
    //   type: Boolean
    // },
    // pid: {
    //   type: String
    // },
    isEdit: {
      type: Boolean
    },
    row: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      addDialogVisible: false,
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  watch: {
    isEdit: {
      handler(val) {
        if (val) {
          // this.form = this.ele
          // this.form = { ...this.ele }
          this.roleForm = JSON.parse(JSON.stringify(this.row))
        } else {
          this.roleForm = {
            name: '',
            description: ''
          }
        }
      },
      immediate: true
    }
  },
  created() {},
  methods: {
    hSubmit() {
      this.$refs.roleForm.validate(async(valid) => {
        if (!valid) return
        // this.form.pid = this.pid
        const res = this.isEdit
          ? await updateRole(this.roleForm)
          : await addRole(this.roleForm)
        console.log(res, 123)
        if (res.data.code === 10000) {
          // this.$emit('update:addDialogVisible', false)
          // this.form = {
          //   name: '', // 部门名称
          //   code: '', // 部门编码
          //   manager: '', // 部门管理者
          //   introduce: '', // 部门介绍
          //   pid: ''
          // }
          this.$emit('loadAddDiaDep')
        }
      })
    },
    hSuccess() {
      this.roleForm = {
        name: '',
        description: ''
      }
    }
  }
}
</script>
<style lang="less" scoped></style>
